{include file="public/layout"/}
<body data-type="generalComponents">
<div class="tpl-content-wrapper">
    <ol class="am-breadcrumb">
        <li><a href="javascript:void(0)" onclick="javascript:parent.openItem('welcome|Index');" class="am-icon-home">首页</a></li>
        <li class="am-active">权限</li>
        <li><a href="{:url('system/right_list')}">权限资源列表</a></li>
        <li class="am-active">权限资源管理 - 编辑权限</li>
    </ol>
    <div class="tpl-portlet-components">
        <div class="tpl-block ">
            <div class="am-g tpl-amazeui-form">
                <div class="am-u-sm-12 am-u-md-9">
                    <form class="am-form am-form-horizontal js-ajax-form" action="{:url('system/edit_right')}" method="post">
                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-form-label">所属类型</label>
                            <div class="am-u-sm-9">
                                <select  id="type-select" name="type" onchange="handleTypeChange()">
                                    {volist name="modules" id="module"}
                                    <option value="{$key}" {if condition="$Request.param.type==$key"}selected{/if}>{$module.title} - {$module.name}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>

                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-form-label">权限资源名称</label>
                            <div class="am-u-sm-9">
                                <input type="text" value="{$info.name ?? ''}" name="name" id="name" class="required">
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-form-label">所属分组</label>
                            <div class="am-u-sm-9">
                                <select id="group" name="group">
                                    {foreach name="group" item="vo" key="key"}
                                    <option value="{$key}" {if condition="isset($info['group']) && $info['group'] eq $key"}selected{/if}>{$vo}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-form-label">添加权限码</label>
                            <div class="am-u-sm-9">
                                <div class="am-u-sm-3" style="padding-left: 0;">
                                    <select id="controller" onchange="get_act_list(this)">
                                        <option value="">选择控制器</option>
                                        {foreach name="planList" item="vo"}
                                        <option value="{$vo}">{$vo}</option>
                                        {/foreach}
                                    </select>
                                </div>
                                <div class="am-u-sm-9">
                                    <ul class="ncap-account-container-list">
                                    </ul>
                                </div>
                            </div>

                        </div>
                        <div class="am-form-group">
                            <label class="am-u-sm-3 am-form-label">权限码</label>
                            <div class="am-u-sm-9">
                                <table>
                                    <tr><th style="width:80%; text-align: left" class="am-form-label">权限码</th><th style="width: 50px;text-align: center;" class="am-form-label">操作</th></tr>
                                    <tbody id="rightList">
                                    {if condition="isset($info)"}
                                        {foreach name="info['right']" item="vo"}
                                            <tr id="{:str_replace('@','_',$vo)}">
                                                <td><input name="right[]" type="text" value="{$vo}" class="form-control" style="width:300px;"></td>
                                                <td style="text-align: center;"><a class="am-btn am-btn-link" href="javascript:;" onclick="$(this).parent().parent().remove();">删除</a></td>
                                            </tr>
                                        {/foreach}
                                    {/if}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="am-form-group">
                            <div class="am-u-sm-9 am-u-sm-push-3">
                                <button type="submit" class="am-btn am-btn-primary js-ajax-submit">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="__ADMIN__/js/admin.js"></script>
<script type="text/javascript">
    function chkbox_bind(){
        $('input:checkbox').change(function () {
            var is_check = $(this).prop('checked');
            var ncode = $('#controller').val();
            var row_id = ncode+'_'+ $(this).val();
            if(is_check){
                var a = [];
                $('#rightList .form-control').each(function(i,o){
                    if($(o).val() != ''){
                        a.push($(o).val());
                    }
                });
                if(ncode !== ''){
                    var temp = ncode+'@'+ $(this).val();
                    if($.inArray(temp,a) != -1){
                        return ;
                    }
                }else{
                    layer.alert("请选择控制器" , {icon:2,time:1000});
                    return;
                }
                var strtr = "<tr id="+row_id+">";
                if(ncode!= ''){
                    strtr += '<td><input type="text" name="right[]" value="'+ncode+'@'+ $(this).val()+'" class="form-control" style="width:300px;"></td>';
                }else{
                    strtr += '<td><input type="text" name="right[]" value="" class="form-control" style="width:300px;"></td>';
                }
                strtr += '<td style="text-align: center;"><a href="javascript:;" class="am-btn am-btn-link" onclick="$(this).parent().parent().remove();">删除</a></td>';
                $('#rightList').append(strtr);
            }else{
                $("#"+row_id).remove();
            }
        });
    }
    chkbox_bind();
    function get_act_list(obj){
        $.ajax({
            url: "{:url('system/ajax_get_action',array('type'=>$type))}",
            type:'get',
            data: {'controller':$(obj).val()},
            dataType:'html',
            success:function(res){
                $('.ncap-account-container-list').empty().append(res);
                chkbox_bind();
                updateActCheck();
            }
        });
    }
    function updateActCheck() {
        var acts = $('input.form-control');
        var controller = $('#controller').val();
        $('input:checkbox').each(function(){
            var act = controller +'@'+ $(this).val();
            for (var i = 0; i < acts.length; i++) {
                if ($(acts[i]).val() === act) {
                    $(this).attr('checked', true);
                    break;
                }
            }
        });
    }
    function adsubmit(){
        if($('input[name=name]').val() == ''){
            layer.msg('权限名称不能为空！', {icon: 2,time: 1000});
            return false;
        }

        if($('input[name="right[]"').val() == ''){
            layer.msg('权限码不能为空！', {icon: 2,time: 1000});
            return false;
        }

        $('#adminHandle').submit();
    }

    function handleTypeChange() {
        var type = $('#type-select').val();
        window.location.href = "{:url('edit_right')}/type/" + type;
    }
</script>
</body>
</html>